﻿package mortal.game.view.copy.QuestionCopy
{
    import Message.Command.*;
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import extend.language.*;
    import flash.geom.*;
    import flash.utils.*;
    import frEngine.event.*;
    import mortal.common.*;
    import mortal.common.tools.*;
    import mortal.game.cache.*;
    import mortal.game.control.subControl.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.events.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.utils.*;
    import mortal.game.view.activeTime.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.copy.QuestionCopy.data.*;
    import mortal.game.view.copy.QuestionCopy.item.*;
    import mortal.game.view.copy.QuestionCopy.panel.*;
    import mortal.game.view.guildNew.*;
    import mortal.mvc.core.*;

    public class QuestionCopyController extends CopyControllerBase
    {
        private var _firstEnterFlag:Boolean = true;
        private var _secTime:SecTimer;
        private var _isInMap:Boolean = false;
        private var _isEnterShow:Boolean = false;
        private var _state:int;
        private var _enterTimer:SecTimer;
        private var _autoOpenLeft:int;
        private var _otim:uint = 0;
        private var _randPos:Array;

        public function QuestionCopyController()
        {
            this._randPos = [[[820, 550], [820, 780], [820, 1030]], [[1300, 550], [1300, 780], [1300, 1030]]];
            return;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.ActiveNotic, this.activesNoticHandler);
            Dispatcher.addEventListener(EventName.ActiveStart, this.activesStarHandler);
            Dispatcher.addEventListener(EventName.ActiveEnd, this.activesEndHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicQuestionBegin, this.onQuestonBegin);
            EventUtil.addNetEvent(ServerCommand.QuestionOpen, this.onQuestionBeginHd, true);
            EventUtil.addSelfEvent(EventName.CopyQuestionOpenEnter, this.onOpenEnterWin, true);
            EventUtil.addSelfEvent(EventName.NavbarButtonUIRowInited, this.initHd, true);
            EventUtil.addNetEvent(ServerCommand.QuestionClose, this.questionOver, true);
            EventUtil.addSelfEvent(EventName.CopyQuestioReqSynTime, this.onSendSynTimeReq, true);
            LevelCheckUtil.addLevelUp(this.onLevelUp);
            return;
        }// end function

        private function addEnterCopyEvens(param1:Boolean) : void
        {
            EventUtil.addSelfEvent(EventName.CopyQuestionRequestEnter, this.onEnterCopyReq, param1);
            EventUtil.addNetEvent(ServerCommand.Copy_EnterCopy, this.myEnterCopy, param1);
            return;
        }// end function

        private function addQuestEvts(param1:Boolean) : void
        {
            EventUtil.addNetEvent(ServerCommand.QuestionScoreUpdate, this.scoreUpdate, param1);
            EventUtil.addNetEvent(ServerCommand.QuestionShowAnswer, this.showAnswer, param1);
            EventUtil.addNetEvent(ServerCommand.QuestionShowPass, this.showPassWin, param1);
            EventUtil.addSelfEvent(EventName.CopyQuestioShowRightEffect, this.onShowRightEffect, param1);
            EventUtil.addSelfEvent(EventName.CopyQuestionMakeChioce, this.onMakeChoiceHd, param1);
            EventUtil.addSelfEvent(EventName.CopyQuestionUseProp, this.useTimesProp, param1);
            EventUtil.addSelfEvent(EventName.CopyQuestionUpdateTimes, this.useTimesPropBack, param1);
            EventUtil.addSelfEvent(EventName.CopyQuestioCountDown, this.onTimeCountDown, param1);
            if (param1)
            {
                FrEventDispatcher.instance.proxyAddEventListener(RolePlayer.instance, PlayerEvent.GIRD_WALK_END, this.pointChangeHandler);
                FrEventDispatcher.instance.proxyAddEventListener(RolePlayer.instance, PlayerEvent.JumpPointEnd, this.jumpEndHandler);
            }
            else
            {
                FrEventDispatcher.instance.proxyRemoveEventListener(RolePlayer.instance, PlayerEvent.GIRD_WALK_END, this.pointChangeHandler);
                FrEventDispatcher.instance.proxyRemoveEventListener(RolePlayer.instance, PlayerEvent.JumpPointEnd, this.jumpEndHandler);
            }
            return;
        }// end function

        private function onLevelUp(param1:Object) : void
        {
            if (this._isEnterShow && this.isInTime && LevelCheckUtil.isLevelEnougth(LevelCheckUtil.question))
            {
                QuestionCopyIcon.instance.show();
                QuestionCopyIcon.instance.sectimerView.visible = false;
            }
            return;
        }// end function

        private function isInTime() : Boolean
        {
            var _loc_1:Boolean = false;
            var _loc_2:* = ActionTimeShowUtil.instance.QuestionArr[0];
            var _loc_3:* = ActionTimeShowUtil.instance.QuestionArr[1];
            var _loc_4:* = DateUtil.getDateByHourAndMin(_loc_2);
            var _loc_5:* = DateUtil.getDateByHourAndMin(_loc_3);
            _loc_1 = ClockManager.instance.nowDate.time > _loc_4.time && ClockManager.instance.nowDate.time < _loc_5.time;
            return _loc_1;
        }// end function

        private function showInfoPanel(param1:Boolean) : void
        {
            EventUtil.addSelfEvent(EventName.CopyQuestioInfoTipBtnCk, this.onTipPanelShow, param1);
            if (param1)
            {
                QuestionCopyInfoPanel.instance.show();
                this.scoreUpdate(null);
            }
            else
            {
                QuestionCopyInfoPanel.instance.hide();
            }
            return;
        }// end function

        private function showCountDownPanel(param1:Boolean) : void
        {
            if (param1)
            {
                if (QuestionCopyDataUtil.canShowCountDownPane())
                {
                    QuestionCopyCountDownPanel.instance.show();
                    QuestionCopyIcon.instance.sectimerView.addEventListener(EventName.SecViewTimeChange, this.onCountDown);
                }
                else if (QuestionCopyCountDownPanel.instance.isHide == false)
                {
                    QuestionCopyCountDownPanel.instance.hide();
                    QuestionCopyIcon.instance.sectimerView.removeEventListener(EventName.SecViewTimeChange, this.onCountDown);
                }
            }
            else if (QuestionCopyCountDownPanel.instance.isHide == false)
            {
                QuestionCopyCountDownPanel.instance.hide();
                QuestionCopyIcon.instance.sectimerView.removeEventListener(EventName.SecViewTimeChange, this.onCountDown);
            }
            return;
        }// end function

        private function onCountDown(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            if (!QuestionCopyDataUtil.canShowCountDownPane())
            {
                this.showCountDownPanel(false);
                if (QuestionCopyInfoPanel.hasInstance)
                {
                    QuestionCopyInfoPanel.instance.updateVip(null);
                }
            }
            else
            {
                QuestionCopyCountDownPanel.instance.showCountDown(_loc_2);
            }
            return;
        }// end function

        private function addQuestionSyn(param1:Boolean) : void
        {
            if (param1)
            {
                if (NetDispatcher.hasCmdListener(ServerCommand.QuestionQuestion) == false)
                {
                    EventUtil.addNetEvent(ServerCommand.QuestionQuestion, this.onQuestionSyn, true);
                }
            }
            else if (NetDispatcher.hasCmdListener(ServerCommand.QuestionQuestion))
            {
                EventUtil.addNetEvent(ServerCommand.QuestionQuestion, this.onQuestionSyn, false);
            }
            return;
        }// end function

        private function onSendSynTimeReq(param1:Object) : void
        {
            GameProxy.question.getQuestionMsgasync();
            return;
        }// end function

        private function activesNoticHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type != EActiveType._EActiveTypeQuestion)
            {
                return;
            }
            var _loc_3:* = _loc_2.startDt.time - ClockManager.instance.nowDate.time;
            if (_loc_3 <= 0)
            {
                return;
            }
            if (this._state == 2)
            {
                return;
            }
            QuestionCopyDataUtil.showCountFlag = false;
            if (LevelCheckUtil.isLevelEnougth(LevelCheckUtil.question))
            {
                QuestionCopyIcon.instance.show();
                QuestionCopyIcon.instance.updateOpenDt(_loc_2.startDt, 1);
                QuestionCopyDataUtil.showCountFlag = true;
            }
            else if (QuestionCopyIcon.hasInstance())
            {
                QuestionCopyIcon.instance.hide();
            }
            return;
        }// end function

        private function activesStarHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type != EActiveType._EActiveTypeQuestion)
            {
                return;
            }
            if (!LevelCheckUtil.isLevelEnougth(LevelCheckUtil.question))
            {
                return;
            }
            var _loc_3:* = _loc_2.endDt.time - ClockManager.instance.nowDate.time;
            if (_loc_3 <= 0)
            {
                return;
            }
            this._state = 2;
            QuestionCopyIcon.instance.show();
            QuestionCopyIcon.instance.updateOpenDt(_loc_2.endDt, 2);
            return;
        }// end function

        private function onQuestonBegin(param1:Object) : void
        {
            var _loc_2:* = ActiveTimeUtil.getSClientActive(EActiveType._EActiveTypeQuestion);
            if (_loc_2 == null)
            {
                return;
            }
            if (LevelCheckUtil.isLevelEnougth(LevelCheckUtil.question))
            {
                this._state = 2;
                QuestionCopyIcon.instance.show();
                QuestionCopyIcon.instance.updateOpenDt(_loc_2.endDt, 2);
            }
            return;
        }// end function

        private function activesEndHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type != EActiveType._EActiveTypeQuestion)
            {
                return;
            }
            this._state = 0;
            QuestionCopyIcon.instance.stopEffect();
            QuestionCopyIcon.instance.hide();
            return;
        }// end function

        private function checkQusState(param1:Date, param2:Date) : void
        {
            QuestionCopyDataUtil.showCountFlag = false;
            var _loc_3:* = ActiveTimeUtil.getSClientActive(EActiveType._EActiveTypeQuestion);
            if (_loc_3 == null)
            {
                return;
            }
            this._state = 2;
            if (LevelCheckUtil.isLevelEnougth(LevelCheckUtil.question))
            {
                QuestionCopyIcon.instance.show();
                if (this._state == 1)
                {
                    QuestionCopyIcon.instance.updateOpenDt(_loc_3.startDt, this._state);
                }
                else
                {
                    QuestionCopyIcon.instance.updateOpenDt(_loc_3.endDt, this._state);
                    QuestionCopyDataUtil.showCountFlag = false;
                }
            }
            else if (QuestionCopyIcon.hasInstance())
            {
                QuestionCopyIcon.instance.hide();
            }
            return;
        }// end function

        private function onStar(param1:Array) : void
        {
            var _loc_2:Date = null;
            if (LevelCheckUtil.isLevelEnougth(LevelCheckUtil.question))
            {
                _loc_2 = param1[1];
                QuestionCopyIcon.instance.show();
                QuestionCopyIcon.instance.updateOpenDt(_loc_2, 2);
            }
            return;
        }// end function

        private function onQuestionBeginHd(param1:Object) : void
        {
            this.showIcon(true);
            return;
        }// end function

        private function onTimeCountDown(event:DataEvent) : void
        {
            var _loc_2:int = 0;
            if (QuestionCopyChoicePanel.instance.isHide == false)
            {
                _loc_2 = Cache.instance.question.curQuesInfo.surplusSeconds;
                QuestionCopyChoicePanel.instance.showTime();
                QuestionCopyChoicePanel.instance.updateYouChoice();
            }
            return;
        }// end function

        private function onTipPanelShow(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            QuestionCopyPassWin.instance.show();
            QuestionCopyPassWin.instance.updatePass(Cache.instance.question.playInfo);
            return;
        }// end function

        private function initHd(param1:Object) : void
        {
            this.checkShowQuestionIcon();
            this._isInMap = true;
            if (cache.copy.copyInfo == null || ECopyType._ECopyTypeQuestion != cache.copy.copyInfo.type)
            {
                return;
            }
            this.showChoiceAndInfoPanel(true, true);
            this.showInfoPanel(true);
            this.showCountDownPanel(true);
            if (CopyUtil.isQuestionCopy)
            {
                EventUtil.addNetEvent(ServerCommand.QuestionQuestion, this.onQuestionSyn, true);
            }
            return;
        }// end function

        private function checkShowQuestionIcon(param1:Object = null) : void
        {
            return;
        }// end function

        private function showIcon(param1:Boolean) : void
        {
            var _loc_2:int = 0;
            var _loc_3:TCopy = null;
            if (param1)
            {
                if (LevelCheckUtil.isLevelEnougth(LevelCheckUtil.question))
                {
                    QuestionCopyIcon.instance.show();
                    this._isEnterShow = false;
                }
                else
                {
                    this._isEnterShow = true;
                }
                if (Cache.instance.question.openS)
                {
                    this._autoOpenLeft = QuestionCopyIcon.instance.leftSecond;
                    if (Cache.instance.question.hasAutoOpenEnter == false && this._autoOpenLeft >= 60)
                    {
                        if (this._autoOpenLeft >= 0 && this._autoOpenLeft <= 60)
                        {
                            Cache.instance.question.hasAutoOpenEnter = true;
                            if (QuestionCopyEnterWindow.instance.isHide)
                            {
                                this._isEnterShow = true;
                                _loc_2 = Cache.instance.role.entityInfo.level;
                                _loc_3 = CopyConfig.instance.getCopyInfoByCode(127);
                                if (_loc_2 >= _loc_3.enterMinLevel && !QuestionCopyInfoPanel.isInView)
                                {
                                    QuestionCopyEnterWindow.instance.show();
                                    this.addEnterCopyEvens(true);
                                    this._isEnterShow = false;
                                }
                                else
                                {
                                    this._isEnterShow = true;
                                }
                            }
                        }
                        else if (this._autoOpenLeft > 60)
                        {
                            if (this._enterTimer == null)
                            {
                                this._enterTimer = new SecTimer();
                            }
                            this._enterTimer.addListener(TimerType.ENTERFRAME, this.onOpenAutHd);
                            this._enterTimer.start();
                        }
                    }
                }
            }
            else
            {
                if (this._enterTimer)
                {
                    this._enterTimer.reset();
                    this._enterTimer = null;
                }
                QuestionCopyIcon.instance.hide();
                this._isEnterShow = false;
            }
            return;
        }// end function

        private function onOpenAutHd(param1:Object) : void
        {
            if (QuestionCopyIcon.instance.sectimerView == null)
            {
                return;
            }
            this._autoOpenLeft = QuestionCopyIcon.instance.sectimerView.getLeftTime();
            if (Cache.instance.question.hasAutoOpenEnter == false && this._autoOpenLeft >= 0 && this._autoOpenLeft <= 60)
            {
                Cache.instance.question.hasAutoOpenEnter = true;
                if (QuestionCopyEnterWindow.instance.isHide && LevelCheckUtil.isLevelEnougth(LevelCheckUtil.question) && !QuestionCopyInfoPanel.isInView)
                {
                    QuestionCopyEnterWindow.instance.show();
                    this.addEnterCopyEvens(true);
                }
                this._enterTimer.stop();
            }
            return;
        }// end function

        private function onOpenEnterWin(param1:Object = null) : void
        {
            if (getTimer() - this._otim < 20)
            {
                return;
            }
            this._otim = getTimer();
            if (QuestionCopyIcon.hasInstance() && !QuestionCopyIcon.instance.isHide)
            {
                if (QuestionCopyEnterWindow.instance.isHide)
                {
                    if (LevelCheckUtil.isLevelEnougth(LevelCheckUtil.question))
                    {
                        QuestionCopyEnterWindow.instance.show();
                        this.addEnterCopyEvens(true);
                        this._isEnterShow = false;
                    }
                    else
                    {
                        this._isEnterShow = true;
                    }
                }
                else
                {
                    this.addEnterCopyEvens(false);
                    QuestionCopyEnterWindow.instance.hide();
                }
            }
            else
            {
                MsgManager.showRollTipsMsg("" + Language.getString(70968));
            }
            return;
        }// end function

        private function onEnterCopyReq(param1:Object) : void
        {
            if (LevelCheckUtil.isLevelEnougth(LevelCheckUtil.question) == false)
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(70969, LevelCheckUtil.getLevel(LevelCheckUtil.question)));
                return;
            }
            CopyUtil.copyCheck(this.enterAfterCheck);
            return;
        }// end function

        private function enterAfterCheck() : void
        {
            GameProxy.copy.enterQuestionCopy();
            return;
        }// end function

        public function myEnterCopy(param1:Object) : void
        {
            if (ECopyType._ECopyTypeQuestion != cache.copy.copyInfo.type)
            {
                return;
            }
            if (QuestionCopyEnterWindow.instance.isHide == false)
            {
                this.addEnterCopyEvens(false);
                QuestionCopyEnterWindow.instance.hide();
            }
            this.showChoiceAndInfoPanel(true, true);
            this.showInfoPanel(true);
            this.showCountDownPanel(true);
            EventUtil.addNetEvent(ServerCommand.QuestionQuestion, this.onQuestionSyn, true);
            Cache.instance.question.curPosValue = -1;
            this._firstEnterFlag = false;
            return;
        }// end function

        private function showChoiceAndInfoPanel(param1:Boolean, param2:Boolean = false) : void
        {
            if (param1)
            {
                if (QuestionCopyChoicePanel.instance.isHide)
                {
                    if (QuestionCopyDataUtil.canShowChoicePanel())
                    {
                        QuestionCopyChoicePanel.instance.show();
                        this.addQuestEvts(true);
                        this.showInfoPanel(true);
                        if (param2)
                        {
                            this.onQuestionSyn(null);
                        }
                    }
                }
            }
            else if (QuestionCopyChoicePanel.instance.isHide == false)
            {
                this.addQuestEvts(false);
                QuestionCopyChoicePanel.instance.hide();
            }
            return;
        }// end function

        override public function leaveCopy() : void
        {
            super.leaveCopy();
            this.showChoiceAndInfoPanel(false);
            this.showInfoPanel(false);
            this.showCountDownPanel(false);
            EventUtil.addNetEvent(ServerCommand.QuestionQuestion, this.onQuestionSyn, false);
            return;
        }// end function

        private function onQuestionSyn(param1:Object) : void
        {
            this.showCountDownPanel(true);
            if (this._isInMap == false)
            {
                return;
            }
            this.updateTitle();
            Cache.instance.question.curPosValue = -1;
            Cache.instance.question.useTimeProType = [0, -1];
            this.pointChangeHandler(null);
            if (Cache.instance.question.playInfo)
            {
                this.showChoiceAndInfoPanel(true, true);
            }
            return;
        }// end function

        private function updateTitle() : void
        {
            if (QuestionCopyDataUtil.canShowChoicePanel())
            {
                if (QuestionCopyChoicePanel.instance.isHide)
                {
                    this.showChoiceAndInfoPanel(true, true);
                }
                QuestionCopyChoicePanel.instance.beginShowTitle(Cache.instance.question.curQuesInfo);
                QuestionCopyInfoPanel.instance.updateLeftNum();
                this.scoreUpdate(null);
            }
            else
            {
                this.showChoiceAndInfoPanel(false);
            }
            return;
        }// end function

        private function scoreUpdate(param1:Object) : void
        {
            var _loc_2:* = QuestionCopyDataUtil.getInfo(Cache.instance.question.playInfo);
            QuestionCopyInfoPanel.instance.updateInfo(_loc_2);
            return;
        }// end function

        private function showAnswer(param1:Object) : void
        {
            if (this._isInMap == false)
            {
                return;
            }
            this.updateTitle();
            return;
        }// end function

        private function onShowRightEffect(param1:Object) : void
        {
            QuestionCopyInfoPanel.instance.showRightEffect();
            return;
        }// end function

        private function showPassWin(param1:Object) : void
        {
            this.showChoiceAndInfoPanel(false);
            QuestionCopyPassWin.instance.show();
            QuestionCopyPassWin.instance.updatePass(Cache.instance.question.passInfo);
            return;
        }// end function

        private function questionOver(param1:Object) : void
        {
            this.showIcon(false);
            if (QuestionCopyEnterWindow.hasInstance() && QuestionCopyEnterWindow.instance.isHide == false)
            {
                QuestionCopyEnterWindow.instance.hide();
            }
            Cache.instance.question.questionEnd();
            this._state = 0;
            return;
        }// end function

        private function onMakeChoiceHd(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            this.movePlayer(_loc_2);
            return;
        }// end function

        private function movePlayer(param1:int) : void
        {
            var _loc_3:Array = null;
            var _loc_2:* = new Point(30, 30);
            _loc_3 = this._randPos[(param1 - 1)];
            _loc_3 = _loc_3[int(Math.random() * 3)];
            _loc_2.x = int(_loc_3[0]) + (-100 + Math.random() * 100);
            _loc_2.y = int(_loc_3[1]) + (-100 + Math.random() * 100);
            Scene3DClickProcessor.gotoPoint(_loc_2, true, -1, false);
            var _loc_4:* = Cache.instance.question.curQuesInfo.questionId;
            GameProxy.question.changeAnswer(_loc_4, param1);
            return;
        }// end function

        private function pointChangeHandler(param1 = null) : void
        {
            this.checkPosition();
            return;
        }// end function

        private function jumpEndHandler(event:ParamsEvent) : void
        {
            this.checkPosition();
            return;
        }// end function

        private function checkPosition() : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_1:* = GameMapUtil.getPixelPointValue(RolePlayer.instance.x2d, RolePlayer.instance.y2d);
            if (_loc_1 != Cache.instance.question.curPosValue)
            {
                Cache.instance.question.curPosValue = _loc_1;
                _loc_2 = Cache.instance.question.curQuesInfo.questionId;
                _loc_3 = Cache.instance.question.curPosValue == 28 ? (1) : (2);
                GameProxy.question.changeAnswer(_loc_2, _loc_3);
            }
            QuestionCopyChoicePanel.instance.updateYouChoice();
            return;
        }// end function

        private function useTimesProp(param1:Object) : void
        {
            if (Cache.instance.question.isOverFlag)
            {
                return;
            }
            var _loc_2:* = Cache.instance.question.curQuesInfo.questionId;
            var _loc_3:* = param1.data as int;
            Cache.instance.question.useTimeProType = [_loc_2, _loc_3];
            GameProxy.question.useTimesProp(_loc_2, _loc_3);
            return;
        }// end function

        private function useTimesPropBack(param1:Object) : void
        {
            this.scoreUpdate(null);
            return;
        }// end function

    }
}
